import pandas as pd
from openpyxl import load_workbook
import os
import openpyxl.utils
from PIL import Image
import io

def return_headers(star_id=None):
    return {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "authorization;": "",
        "priority": "u=1, i",
        "referer": f"https://pgy.xiaohongshu.com/solar/pre-trade/blogger-detail/{star_id}?track_id=homepage_kolmatch_1a5ab1d3c2a14028a7797957cae34507&source=Home-Recommend",
        "sec-ch-ua": "\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
        "x-b3-traceid": "611bf2f11686e5de"
}



def return_post_headers(star_id=None):
    return {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "authorization;": "",
        "priority": "u=1, i",
        "referer": f"https://pgy.xiaohongshu.com/solar/pre-trade/blogger-detail/{star_id}?track_id=homepage_kolmatch_8b24ed45567d40adbd45ccd17c60e188&source=Home-Recommend",
        "sec-ch-ua": "\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
        "x-b3-traceid": "1ee12eb237443a72",
        "x-s": "XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJyYXRsaW4iLCJzaWduVmVyc2lvbiI6IjEiLCJwYXlsb2FkIjoiMmE5ZDRlMjhkYTZlOGIxZDNmNjc5M2E1NDM0MzZjNzkxMjBlMjU0NGJkNDJjNmQ2OGI3ZjcwYjIxMDY2M2IxOTdlZTcyMmM3YThmMTZhZmIzNDU1Y2U5ZTM5ZTczNDY0OTcxZThjN2M2OGFiOWI1MGM1ZDNmY2E0NDk3YzBkNTZmZmMwZmM3ZGQ4YmE0NDUwYjU4MDVlMzU5NDA4NDFiNWE3MTEyNTU0YTdkNjYwNmRkODM1ZWE0NTYzNTVmNmI5ZjhiM2ZlY2IyNTAzYzE1NTFkMjI0MjI0MTkyMDNlN2IwODZjZTE4OTQ0ZGNmZTI4OTAwMmZhZmE5OThjZGQ2NmIwMTQ1ZjNhNzhiY2Q2ODdiMzIyYjk5N2VhZTg0MmQ0NGFhNmU0NDFiMWNjNTMwYTlhYjc4OWE4NzQ3YTk2ZmJlMDQwMzIyMmVkYzg3ZWVkNGQwZWIyNjRjN2VlYzc0NjRjODEyNGM0ZWVlNmJmZDVhOWIxNGUwYTJjM2MyNjA1ODdlMzczNGQ2OTQ1MzE4M2U2YjY0MGUyNjYwYjdkZDcifQ==",
        "x-t": "1750735376743"
    }
def return_content_type_post_headers(star_id=None):
    return {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "authorization;": "",
        "content-type": "application/json;charset=UTF-8",
        "origin": "https://pgy.xiaohongshu.com",
        "priority": "u=1, i",
        "referer": f"https://pgy.xiaohongshu.com/solar/pre-trade/blogger-detail/{star_id}?track_id=homepage_kolmatch_0d260a4839d84a2cb01983a639a8b065&source=Home-Recommend",
        "sec-ch-ua": "\"Google Chrome\";v=\"137\", \"Chromium\";v=\"137\", \"Not/A)Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
        "x-b3-traceid": "8beb060f1caa421c",
        "x-s": "XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJyYXRsaW4iLCJzaWduVmVyc2lvbiI6IjEiLCJwYXlsb2FkIjoiYmY2Mzc0NDJhOTczY2E4NzA5YzM1MDg4MmRhMzk0MDk3MDc2NTBkZDQ1ZWQ0MTJkMDcyNjY5MzcwYmIyNWJiMjBlNDZlYjk3MzBiNmNhNjJkNzE1NjIxYzM5MWM0ZmNiZmFjZWEwMTc4MGE0YmYxNGFmYzY4NDY3NDk2OGYwZjU0MTUxNWNhMjE1Nzc1ZDk2OWU0MTBhMTk2YmQ3YmU5YTIyMDBjZTFiM2NiMmEwNmVkY2VhMWQxZjBlZGFhNjA2Nzc5NDhkZjA5YmE4ZDAzN2I2YzQxMDM4NTU1YTg0ZmUxNjhlMjE1ZjI2OGYyNjQ1ZjhkMTgyZWZjYTE2OWRjZTk2ZDYwZmYzZjdmOTA1OWM0ZTJhNjM1ODIzMDEyMDdjMmUyZjBkMmVhNTU4Y2Q4ZjdkNTFjYzIyZjdjMTVmOWU1OTMyNzE5ZjNkMDRmNDQ4MGMxOTg2MTA3NWMwZGNjMjU2OTNkZjgxNzFmNTU3YmQ1NWNiMzRlZmU4MTBmODQxMThjYjE0MDlmMzU1ZmJhYjJkNTRiM2NmMmM4YTdjMzcifQ==",
        "x-t": "1750748376409"
}

mock_click_js = """
function triggerFullClick(element) {
  // 1. 触发鼠标按下
  element.dispatchEvent(new MouseEvent('mousedown', {
    bubbles: true,
    cancelable: true,
    view: window
  }));

  // 2. 触发获取焦点
  element.focus();
  element.dispatchEvent(new Event('focus', {bubbles: true}));

  // 3. 触发鼠标释放
  element.dispatchEvent(new MouseEvent('mouseup', {
    bubbles: true,
    view: window
  }));

  // 4. 触发最终点击
  element.dispatchEvent(new MouseEvent('click', {
    bubbles: true,
    view: window
  }));
}

// 执行函数
triggerFullClick(this);
"""
# 设置显示所有列（默认只显示部分列）
pd.set_option('display.max_columns', None)

# 设置显示所有行（默认只显示部分行）
pd.set_option('display.max_rows', None)

# 设置列宽（防止内容被截断）
pd.set_option('display.max_colwidth', 100)  # 设置每列最大显示宽度

def get_complete_sheet1(file_path=None):
    """
    读取直达链接预先录入sheet，若不存在返回空DataFrame
    """
    try:
        df = pd.read_excel(file_path)
        return df
    except ValueError as e:
        if "Worksheet named '直达链接预先录入' not found" in str(e):
            print(f"警告: 文件 {file_path} 中不存在[直达链接预先录入]sheet")
            return pd.DataFrame()  # 返回空DF
        else:
            raise
    except Exception as e:
        print(f"读取文件失败: {e}")
        return pd.DataFrame()

